$(function () {
    init_muenid();

});
/**初始化权限树状图**/
function init_muenid() {
    /**菜单权限树状**/
    $.post(ctx + "menu/menuButtonTree", {}, function (r) {
        if (r.code === 0) {
            var data = r.msg;
            $('#menuTree').jstree({
                "core": {
                    "data":data.children
                },
                "checkbox" : {
                    "keep_selected_style" : false
                },
                "plugins" : [ "wholerow", "checkbox" ]
            });
            //init_role_info();
        } else {
            $JuXue.n_danger(r.msg);
        }
    });
}

/**初始化角色数据**/
function init_role_info(){
    var selected = $("#role-data-table").bootstrapTable('getSelections');
    var selected_length = selected.length;
    if (!selected_length) {$JuXue.n_warning('请勾选需要修改的角色！');return;}
    if (selected_length > 1) {$JuXue.n_warning('一次只能修改一个角色！');return;}

    var roleId = selected[0].roleId;
    $("#menuTree").jstree().deselect_all(true);
    $.post(ctx + "role/getRole", {"roleId": roleId}, function (r) {
        if (r.code === 0) {
            var $menuTree = $('#menuTree');
            var role = r.msg;
            $("#roleName").val(role.roleName);
            $("#remark").val(role.remark);
            $("#roleId").val(role.roleId);
            var menuArr = new Array();
            for (var i = 0; i < role.menuIds.length; i++) {
                menuArr.push(role.menuIds[i]);
            }
            $menuTree.jstree().select_node(menuArr,true,true);
        } else {
            $JuXue.n_danger(r.msg);
        }
    });
}

/**保存角色的更新**/
function editRolesInfo() {
    var $menuTree = $('#menuTree');
    var ref = $menuTree.jstree(true);
    var menuIds = ref.get_checked();
    $menuTree.find(".jstree-undetermined").each(function (i, element) {
        menuIds.push($(element).closest('.jstree-node').attr("id"));
    });
    $("#menuId").val(menuIds);
    var $roleAddForm = $("#role-edit-form");
    $.post(ctx + "role/update", $roleAddForm.serialize(), function (r) {
        if (r.code === 0) {
            $JuXue.n_success(r.msg);
            $JuXue.refreshTable("role-data-table");
            $("#Roles_info").modal("hide");
        } else $JuXue.n_danger(r.msg);
    });

}